import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:landlore/consts.dart';
import 'package:landlore/dbUtil/databaseHelper.dart';
import 'package:landlore/screens/roomCheckInOutScreen.dart';
import '../model/roomInfo.dart';
import '../screens/roomEditScreen.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class RoomManageCard extends StatelessWidget {
  final RoomInfo roomInfo;
  final Function callback;
  final Function popCallback;

  const RoomManageCard({
    super.key,
    required this.roomInfo,
    required this.callback, required this.popCallback,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
        color: roomInfo.state==1 ? Colors.white70 : Colors.black12,
        elevation: 10,
        margin:   EdgeInsets.all(12.w),
        child: Row(
          children: [
            Expanded(
                flex: 2,
                child: Container(
                    padding:   EdgeInsets.only(left: 16.w),
                    child: Text(
                      roomInfo.roomNum!,
                      style: Consts.titleText.copyWith(fontSize: 35.sp),
                      overflow: TextOverflow.visible,
                    ))),
              SizedBox(width: 5.w,),
            Expanded(
              flex: 2,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("租金:${roomInfo.rent??''}",style: TextStyle(fontSize: 15.sp),),
                  Text("附加费:${roomInfo.otherCharge??''}",style: TextStyle(fontSize: 15.sp),),
                  Text("联系人:${roomInfo.contact??''}",style: TextStyle(fontSize: 15.sp),),
                ],
              ),
            ),
            Expanded(
              flex: 1,
              child: Column(
                children: [
                  CupertinoSwitch(
                      value: roomInfo.state==1?true:false,
                      onChanged: (value) async {
                        await DatabaseHelper.instance.switchRoomState(roomInfo.id!,value);
                        callback(value);
                      }),
                  PopupMenuButton(
                      onSelected: (String choice) async{
                        if(choice == 'edit'){
                          await Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>RoomEditScreen(roomInfo: roomInfo,)
                              )
                          );
                          //回调函数,通知上层,刷新数据
                          popCallback();

                        }else if(choice == 'checkIn'){
                          await Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>RoomCheckInOutScreen(roomInfo: roomInfo, state: 1,)
                              )
                          );
                          //回调函数,通知上层,刷新数据
                          popCallback();

                        }else if(choice == 'checkOut'){
                          await Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>RoomCheckInOutScreen(roomInfo: roomInfo, state: 0,)
                              )
                          );
                          //回调函数,通知上层,刷新数据
                          popCallback();

                        }else if(choice == 'dele'){

                          showDialog(context: context, builder: (BuildContext context){
                            return AlertDialog(
                              title: const Text('注意'),
                              content:const Text('确定要删除吗'),
                              actions: [TextButton(onPressed: (){

                                //删除房屋订单
                                DatabaseHelper.instance.deleteData(TableName.ORDER, primaryKey: 'roomId', primaryKeyValue: roomInfo.id!);
                                //删除房屋
                                DatabaseHelper.instance.deleteData(TableName.ROOM_INFO, primaryKey: 'id', primaryKeyValue: roomInfo.id!);

                                //回调函数,通知上层,刷新数据
                                popCallback();
                                //关闭弹框
                                Navigator.pop(context);
                              },
                                  child: const Text('确认'))],
                            );
                          });

                        }
                      },
                      itemBuilder: (BuildContext context){
                    return [
                      const PopupMenuItem(value:'edit',child: Text('编辑')),
                      const PopupMenuItem(value:'checkIn',child: Text('入住')),
                      const PopupMenuItem(value:'checkOut',child: Text('退租')),
                      const PopupMenuItem(value:'dele',child: Text('删除')),
                    ];
                  }),
                  // IconButton(
                  //     onPressed: () {
                  //       Navigator.push(
                  //           context,
                  //           MaterialPageRoute(
                  //               builder: (context) => RoomEditScreen(roomInfo: roomInfo,)));
                  //       // Navigator.pushNamed(context, "/roomEditScreen");
                  //     },
                  //     icon: const Icon(Icons.menu_outlined))
                ],
              ),
            )
          ],
        ));
  }
}
